package com.trs.publish.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trs.publish.aspect.Description;
import com.trs.publish.aspect.OperationDesc;
import com.trs.publish.domain.LoginLog;
import com.trs.publish.domain.OperationLog;
import com.trs.publish.service.LoginLogService;
import com.trs.publish.service.OperationLogService;



/**
 * @author jia.haihong
 *
 */
@Controller
public class LogInfoController{ 
	
	private static final Logger logger = LoggerFactory.getLogger(LogInfoController.class);
	
	@Autowired
	private LoginLogService loginLogService;
	
	@Autowired
	private OperationLogService operationLogService;
	
	
	//登录日志跳转
	@RequestMapping(value = "/toLoginLog")
    public ModelAndView loginLog(ModelAndView mav) {
        mav.setViewName("logInfo/loginLog");
        return mav;
    }  
	
	//登录日志查询方法
	@PatchMapping(value = "/loginLog")
    @ResponseBody
    public Object loginLog(@RequestBody LoginLog loginLog) {
		//获取前端检索参数
		String username = loginLog.getUsername();
		String truename = loginLog.getTruename();
		String starTime = loginLog.getStartTime();
		String endTime =loginLog.getEndTime();
		
		LoginLog loginLogPara = new LoginLog();
		loginLogPara.setUsername(username);
		loginLogPara.setTruename(truename);
		loginLogPara.setStartTime(starTime);
		loginLogPara.setEndTime(endTime);
		
		//mybatis分页
		PageHelper.startPage(loginLog);
		List<LoginLog> list = loginLogService.selectAll(loginLogPara);
		
		
		PageInfo<LoginLog> pageInfo = new PageInfo<LoginLog>(list);
        return pageInfo;
    }
	
	//查询单个登录日志
	@GetMapping(value = "/loginLog/{id}")
	public ModelAndView getLogin(ModelAndView mav,@PathVariable("id") long id) {
		logger.debug("查询单个登陆日志id:" + id);
		LoginLog loginLog = new LoginLog();
		if(id == 0) {
			loginLog.setId(id);
		} else {
			loginLog = loginLogService.selectone(id);
		}
		mav.addObject("loginLog",loginLog);
		mav.setViewName("logInfo/loginLogDetail");
		return mav;
	}
		
	
	//操作日志跳转页面
	@RequestMapping(value = "/toOperationLog")
    public ModelAndView operationLog(ModelAndView mav) {
        mav.setViewName("logInfo/operation");
        return mav;
    }  
	
	//操作日志查询方法
	@PatchMapping(value = "/operationLog")
    @ResponseBody
    public Object operationLog(@RequestBody OperationLog operationLog) {
		//获取前端检索参数
		String username = operationLog.getUsername();
		String truename = operationLog.getTruename();
		String starTime = operationLog.getStartTime();
		String endTime =operationLog.getEndTime();
		
		OperationLog operationLogPara = new OperationLog();
		operationLogPara.setUsername(username);
		operationLogPara.setTruename(truename);
		operationLogPara.setStartTime(starTime);
		operationLogPara.setEndTime(endTime);
		
		//mybatis分页
		PageHelper.startPage(operationLog);
		List<OperationLog> list = operationLogService.selectAll(operationLogPara);
		
		
		PageInfo<OperationLog> pageInfo = new PageInfo<OperationLog>(list);
        return pageInfo;
    }
	
	//查询单个操作日志
	@GetMapping(value="/operationLog/{id}")
	public ModelAndView getOperation(ModelAndView mav,@PathVariable("id") long id) {
		logger.debug("查询单个操作日志id：" + id);
		OperationLog operationLog = new OperationLog();
		if(id==0) {
			operationLog.setId(id);
		}else {
			operationLog = operationLogService.selectone(id);
		}
		mav.addObject("operationLog",operationLog);
		mav.setViewName("logInfo/operationLogDeatil");
		return mav;
	}

	
			
}

